"""
Case Type   : 兼容日期函数
Case Name   : 非严格模式下adddate()函数第一参数指定time类型边界外值，第二参数带interval
              输入正确
Create At   : 2023.03.15
Owner       : @zou_jialiang0501129488
Description :
    1、创建兼容b库并查询插件是否加载成功
    2、非严格模式下adddate()函数第一参数指定time类型边界外值，第二参数带interval输入正确
    3、清理环境
Expect      :
    1、成功，插件查询成功
    2、合理报错
    3、清理成功
History     :modified by z@wan005，2023.09.26，select场景不区分严格模式和非严格模式
             modified by zhanghuan96，2024.06.28，报错提示变更
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest01(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_adddate_0026'

    def test_create_function(self):
        text = '-----step1:创建兼容b库库并查询插件;expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility ='B';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS,
                      sql_res, '兼容B库创建失败')

        sql_cmd1 = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd1)
        sql_res1 = self.sh_primary.execut_db_sql(sql_cmd1, dbname=self.db_name)
        self.log.info(sql_res1)
        self.assertIn('dolphin', sql_res1, '查询失败')

        text = '-----step2:非严格模式下adddate()函数第一参数指定time类型边界外值，' \
               '第二参数带interval输入正确; expect:合理报错-----'
        self.log.info(text)
        sql_cmd2 = f"set dolphin.sql_mode = " \
                   f"sql_mode_full_group,pipes_as_concat," \
                   f"ansi_quotes,no_zero_date;" \
                   f"select adddate(time'839:00:00', interval -1 day);" \
                   f"select adddate(time'-839:00:00', interval 1 day);"
        self.log.info(sql_cmd2)
        res = self.sh_primary.execut_db_sql(sql_cmd2, dbname=self.db_name)
        self.log.info(res)
        self.assertIn('SET', res, 'sql_mode设置失败')
        self.assertEqual(2, res.count('ERROR:'), '查询失败')

    def tearDown(self):
        text = '-----step3:清理环境;expect:成功-----'
        self.log.info(text)
        sql_cmd4 = f"drop database if exists {self.db_name};"
        self.log.info(sql_cmd4)
        sql_res4 = self.sh_primary.execut_db_sql(sql_cmd4)
        self.log.info(sql_res4)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS,
                      sql_res4, '清理环境失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
